package com.medicine.hbp.ui;

import java.util.Set;
import java.util.regex.Matcher;

import org.json.JSONException;
import org.json.JSONObject;
import org.kymjs.kjframe.KJHttp;
import org.kymjs.kjframe.http.HttpCallBack;
import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.BindView;

import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

import com.medicine.hbp.R;
import com.medicine.hbp.base.BaseActivity;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.http.LoginAppHttpCallBack;
import com.medicine.hbp.mob.LoginUserVo;
import com.medicine.hbp.sp.LoginSp;
import com.medicine.hbp.uitl.MD5;
import com.medicine.hbp.uitl.PatternUtils;
import com.medicine.hbp.uitl.ToastUtil;
import com.umeng.socialize.utils.Log;

/**
 * 注册界面
 *
 * @author Administrator
 *
 */
public class RegisterActivity extends BaseActivity {
	@BindView(id = R.id.text_title, click = false)
	public TextView text_title;
	@BindView(id = R.id.btn_image_left_exit, click = true)
	public ImageView btn_image_left_exit;

	/** 用户名 */
	@BindView(id = R.id.edit_name_r)
	public EditText edit_name_r;
	/** 手机号 */
	@BindView(id = R.id.edit_phone_r)
	public EditText edit_phone_r;
	/** 验证码 */
	@BindView(id = R.id.edit_yanzheng_code)
	public EditText edit_yanzheng_code;
	/** 用户密码 */
	@BindView(id = R.id.edit_password_r)
	public EditText edit_password_r;
	/** 用户密码 */
	@BindView(id = R.id.edit_password_yes)
	public EditText edit_password_yes;
	/** 省 */
	@BindView(id = R.id.edit_peovince)
	public EditText edit_peovince;
	/** 市 */
	@BindView(id = R.id.edit_city)
	public EditText edit_city;
	/** 区 */
	@BindView(id = R.id.edit_area)
	public EditText edit_area;
	/** 详细地址 */
	@BindView(id = R.id.edit_address)
	public EditText edit_address;
	/** 提交按钮 */
	@BindView(id = R.id.btn_register, click = true)
	public Button btn_register;
	/** 获取验证码 */
	@BindView(id = R.id.btn_smscode, click = true)
	public Button btn_smscode;

	String country="";// 国家
	String area="";// 区域
	String region="";// 省
	String city="";// 市
	String county="";// 区县
	String isp="";// 运营商

	private TimeCount time;

	@Override
	public void setRootView() {
		super.setRootView();
		setContentView(R.layout.activity_register);
	}

	@Override
	public void initWidget() {
		super.initWidget();
		text_title.setText("快速注册");
		time = new TimeCount(60000, 1000);
		httpAddress();
	}

	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
		switch (v.getId()) {
			case R.id.btn_register:
				httpReginUser();
				break;
			case R.id.btn_smscode:
				httpSmsCode();
				break;
			case R.id.btn_image_left_exit:
				finish();
				break;

			default:
				break;
		}
	}

	/**
	 * 短信发送
	 */
	private void httpSmsCode() {
		String phone=edit_phone_r.getText().toString();
		Matcher matcherMailbox = PatternUtils.patternNumber
				.matcher(phone);
		if (!matcherMailbox.matches()) {
			ToastUtil.showShort(aty, "请输入正确的手机号");
			return;
		}
		HttpParams params = new HttpParams();
		params.put("phone", phone);// 用户账户
		params.put("smsType", "0");// 1找回密码短信 0注册短信
		AppHttps.HttpPost(this,HttpInterface.SMS_CODE, params,
				new AppHttpCallBack<Object>(Object.class, aty) {
					@Override
					public void onSuccess(Object result) {
						ToastUtil.showShort(aty, "短信已发送,请注意查收");
						time.start();// 开始计时
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}
				});

	}

	/**
	 * 注册接口
	 */
	private void httpReginUser() {
		Matcher matcherMailbox = PatternUtils.patternNumber.matcher(edit_phone_r.getText().toString());
		if (!matcherMailbox.matches()) {
			ToastUtil.showShort(aty, "请输入正确的手机号");
			return;
		}
		if(TextUtils.isEmpty(edit_name_r.getText().toString())){
			ToastUtil.showShort(aty, "请输入您的昵称");
			return;
		}
		if(TextUtils.isEmpty(edit_yanzheng_code.getText().toString())){
			ToastUtil.showShort(aty, "请输入验证码");
			return;
		}
		if(TextUtils.isEmpty(edit_password_r.getText().toString())){
			ToastUtil.showShort(aty, "请输入密码");
			return;
		}
		if(TextUtils.isEmpty(edit_password_yes.getText().toString())){
			ToastUtil.showShort(aty, "请输入确认密码");
			return;
		}
		HttpParams params = new HttpParams();
		params.put("userName", edit_name_r.getText().toString());// 昵称
		params.put("account", edit_phone_r.getText().toString());// 用户账户
		params.put("password", edit_password_r.getText().toString());// 用户密码
		params.put("confirmPass", edit_password_yes.getText().toString());// 确认密码
		params.put("code", edit_yanzheng_code.getText().toString());// 验证码
		params.put("province", region);// 省
		params.put("city", city);// 市
		params.put("county", county);// 区县
		params.put("area", area);// 详细地址
		AppHttps.HttpPost(this,HttpInterface.REGIN_USER, params,
				new AppHttpCallBack<Object>(Object.class, aty) {
					@Override
					public void onSuccess(Object result) {
						ToastUtil.showShort(aty, "注册成功");
						httpLoginUser(edit_phone_r.getText().toString(), edit_password_r.getText().toString());
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}
				});

	}
	/**
	 * 登陆接口
	 */
	private void httpLoginUser(String name, String psd) {
		HttpParams params = new HttpParams();
		params.put("account", name);// 用户账户(测试接口正确为user)
		params.put("password", MD5.getMD5String(MD5.getMD5String(psd)));// 用户密码(测试接口正确为pass)
//		params.put("password", psd);// 用户密码(测试接口正确为pass)
		AppHttps.HttpPost(this,HttpInterface.LOGIN_USER, params,
				new LoginAppHttpCallBack<LoginUserVo>(LoginUserVo.class, aty) {
					@Override
					public void onSuccess(LoginUserVo result) {
						if (result.userInfo != null) {
							LoginSp.setUser(result.userInfo, RegisterActivity.this);
							Intent intent = new Intent();
							intent.putExtra("isTab", true);
							setResult(RESULT_OK, intent);
							ToastUtil.showShort(aty, "登录成功");
							JPushInterface.setAliasAndTags(aty, "com.medicine.hbp"+LoginSp.getUserId(aty), null, new TagAliasCallback() {

								@Override
								public void gotResult(int arg0, String arg1, Set<String> arg2) {
									if(arg0==0){
									}

								}
							});
							ToastUtil.showShort(aty, "恭喜您获得"+20+"积分");
							finish();
						}
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
						Log.e("errorCode", errorCode + ":" + errorMsg);
					}
				});
	}
	/**
	 * 地市信息获取
	 */
	private void httpAddress() {
		KJHttp http = new KJHttp();
		http.get(HttpInterface.Address, new HttpCallBack() {
			@Override
			public void onSuccess(String t) {
				super.onSuccess(t);
				try {
					JSONObject object = new JSONObject(t);
					JSONObject data = object.getJSONObject("data");
					country = data.getString("country");
					area = data.getString("area");
					region = data.getString("region");
					city = data.getString("city");
					isp = data.getString("isp");
					county = data.getString("county");
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			@Override
			public void onFailure(int errorCode, String errorMsg) {
//				ToastUtil.showShort(aty, errorMsg);
			}
		});
	}



	class TimeCount extends CountDownTimer {
		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onFinish() {// 计时完毕
			btn_smscode.setText("获取验证码");
			btn_smscode.setClickable(true);
		}

		@Override
		public void onTick(long millisUntilFinished) {// 计时过程
			btn_smscode.setClickable(false);// 防止重复点击
			btn_smscode.setText(millisUntilFinished / 1000 + "s");
		}
	}
}
